﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommunityPages;
using Orchard.DisplayManagement.Descriptors;
using Orchard.Environment.Extensions;
using Orchard.UI.Zones;
using Orchard.ContentManagement.Handlers;
using Orchard;
using Orchard.ContentManagement;
using So.ShapeViewCounter.Services;

namespace CommunityPages
{
    public class ZoneShapes : IShapeTableProvider {
        private readonly IShapeUserViewCounterService _shapeUserViewCounterService;
        private readonly IWorkContextAccessor _workContextAccessor;

        public ZoneShapes(IWorkContextAccessor workContextAccessor
            ,IShapeUserViewCounterService shapeUserViewCounterService
            ) {
            _shapeUserViewCounterService = shapeUserViewCounterService;
            _workContextAccessor = workContextAccessor;
        }

        public void Discover(ShapeTableBuilder builder)
        {
            builder.Describe("Parts_ExampleShape").OnDisplaying(displaying =>
            {
                var shapeUserViewCounterService = _workContextAccessor.GetContext().Resolve<IShapeUserViewCounterService>();
                var shape = displaying.Shape;
                int contentItemid = 1;
                var currentUser = _workContextAccessor.GetContext().CurrentUser;

                if (contentItemid > 0 && currentUser != null)
                {
                    shapeUserViewCounterService.UpSertShapeUserView("Parts_ExampleShape", contentItemid, currentUser.Id);
                }
            });
        }
    }
}
